<style type="text/css">
    .table thead th {
        vertical-align: middle;
    }
    table td.ayat-arabic {
        font-family: myFirstFont;
        text-align: center;
        font-size: 25px;
        letter-spacing: 1px;
        vertical-align: middle;
        weight: 10%;
        line-height:40px;
    }
    table td.ayat {
        text-align: center;
        vertical-align: middle;
        weight: 10%;
        line-height:40px;
    }
</style>
        
<div class="row">
    <div class="col-lg-12">
        <h1 class="page-header">Home</h1>
    </div>
</div>

<form class="form-horizontal" method="post" action="">
    <div class="control-group" style="width: 100%;">
        <label class="control-label" for="pertanyaan">Pertanyaan</label>
        <div class="controls" style="display: inline-block; margin-left: 10px; width:80%;">
            <input style="width: 70%; border-radius: 10px; vertical-align: middle; padding-left: 14px; padding-right: 14px; background-color: #ffffff;
    border: 1px solid #cccccc; box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset; transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s; font-size: 14px;
    height: 35px; line-height: 20px;" type="text" id="pertanyaan" name="keyword" placeholder="Apa Maksud Hari Kiamat?" class="input-xxlarge search-query">
            <button type="submit" name="search" class="btn" style="-moz-border-bottom-colors: none; -moz-border-left-colors: none;
    -moz-border-right-colors: none; -moz-border-top-colors: none; background-color: #f5f5f5; background-image: linear-gradient(to bottom, #ffffff, #e6e6e6);
    background-repeat: repeat-x; border-color: rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.25); border-image: none; border-radius: 15;
    border-style: solid; border-width: 1px; box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2) inset, 0 1px 2px rgba(0, 0, 0, 0.05); color: #333333; cursor: pointer; display: inline-block; font-size: 14px; line-height: 20px; margin-bottom: 0; text-align: center; text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
    vertical-align: middle;">Search</button>
        </div>
    </div>
</form>

<?php 
if (!isset($_SESSION['keyword'])) {
    $_SESSION['keyword'] = '';
}

$_SESSION['keyword'] = isset($_POST['keyword']) ? str_replace("'", "", $_POST['keyword']) : str_replace("'", "", $_SESSION['keyword']);

$preProses      = new PreProcessing($pdo);
$rulePertanyaan = new RulePertanyaanSatu($pdo);

$stemmerFactory = new \Sastrawi\Stemmer\StemmerFactory();
$stemmer        = $stemmerFactory->createStemmer();
$lastResult         = $rulePertanyaan->qas($_SESSION['keyword']);

if (!empty($_SESSION['keyword'])) :
?>
<div class="alert alert-success" style="margin-top:10px;"><?php echo $_SESSION['keyword']; ?></div>
<?php
endif;

if (!is_array($lastResult['data'])):
    echo $lastResult['data'];
endif;
?>
<?php 
if (is_array($lastResult['data'])): 
$result = $lastResult['data'];
$listNamaSuratIndonesia = $lastResult['namaSurat']['indonesia'];
$listNamaSuratArabic = $lastResult['namaSurat']['arabic'];
?>
<table class="table table-striped">
    <thead>
        <tr>
            <th rowspan="2" class="head-table">No</th>
            <th rowspan="2" class="head-table">Ayat</th>
            <th colspan="2" class="head-table">Scoring</th>
            <th rowspan="2" class="head-table">Total Score</th>
        </tr>
        <tr>
            <th class="head-table" style="min-width:125px;">Wordmatch</th>
            <th class="head-table" style="min-width:125px;">Rule</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $no = 1;
        foreach ($result as $dataResult) :
            ?>
            <tr>
                <td><?php echo $no; ?></td>
                <td>
                <?php
                    foreach ($dataResult['data'] as $jenis => $data) :
                        if ($jenis == 'arabic'):
                            echo '<div class="arabic" style="display: inline-block; float:right">';
                            echo $data;
                            echo '</div>';
                            echo '<div class="arabic" style="display: inline-block">( '.$listNamaSuratArabic[$dataResult['sura']].' : '.angkaToArabic($dataResult['aya']).' )</div>';
                        endif;
                    endforeach;
                    
                    echo '<br/>';
                    foreach ($dataResult['data'] as $jenis => $data) :
                        if ($jenis == 'indonesia'):
                            echo $data.' ( '.$listNamaSuratIndonesia[$dataResult['sura']].' : '.$dataResult['aya'].' )';
                        endif;
                    endforeach;
                ?>
                </td>
                <td>
                <?php
                    foreach ($dataResult['data'] as $jenis => $data) :
                        if ($jenis == 'wordmatch'):
                            foreach ($data as $kata => $value) {
                                echo $kata.' : '.$value['score'].'<br/>';
                            }
                        endif;
                    endforeach; //end for dataSura
                ?>
                </td>
                <td>
                <?php
                    foreach ($dataResult['data'] as $jenis => $data) :
                        if ($jenis == 'rule1' || $jenis == 'rule2' || $jenis == 'rule3' || $jenis == 'rule4'):
                            echo $jenis.'<br/>';
                            foreach ($data as $kata => $value) {
                                echo $kata.' : '.$value['score'].'<br/>';
                            }
                            echo '<br/>';
                        endif;
                    endforeach; //end for dataSura
                ?>
                </td>
                <td>
                <?php
                    foreach ($dataResult['data'] as $jenis => $data) :
                        if ($jenis == 'total'):
                            echo $data;
                        endif;
                    endforeach;
                ?>
                </td>
            </tr>
            <?php
            $no++;
        endforeach;
        ?>
    </tbody>
</table>
<?php endif; ?>
